package com.cf.data.serve.environment;

import com.cf.data.serve.entity.LbsjkData;
import com.cf.data.serve.entity.SbLbfh;
import com.cf.data.serve.service.LbsjkDataService;
import com.cf.data.serve.service.SbLbfhService;
import com.cf.data.serve.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

@Slf4j
public class TCPServer_lbfh {
    private static SbLbfhService sbLbfhService = BeanUtils.getBean(SbLbfhService.class);
    public static void lbsjk(int port) throws IOException {
        ServerSocket serverSocket = null;
        try {
            System.out.println("开启端口：8339");
            serverSocket = new ServerSocket(port); // 监听1234端口
            Socket clientSocket = serverSocket.accept(); // 等待客户端连接
            System.out.println("连接成功");

            // 获取输入流
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            // 获取输出流
            OutputStream outputStream = clientSocket.getOutputStream();
            String inputLine;
            //存储单个2进制字符
            String[] cun = null;
            String parseResult = "";
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                cun = inputLine.split(";"); // 将接收到的消息发送回客户端
            }

            if(cun!=null){
                SbLbfh sbLbfh = new SbLbfh();
                sbLbfh.setId(String.valueOf(System.currentTimeMillis()));
                sbLbfh.setStatus("0");
                sbLbfh.setCreateTime(new Date());
                for(int i=0;i<=cun.length;i++){
                    System.out.println(cun[i]);
                    String[] z = cun[i].split(":");
                    switch (z[0]){
                        case "LBFH_HC" :
                            sbLbfh.setLbfhHc(z[1]);
                            break;
                        case "SET_LB1" :
                            sbLbfh.setSetLb1(z[1]);
                            break;
                        case "SET_LB2" :
                            sbLbfh.setSetLb2(z[1]);
                            break;
                        case "SET_H1" :
                            sbLbfh.setSetH1(z[1]);
                            break;
                        case "SET_H2" :
                            sbLbfh.setSetH2(z[1]);
                            break;
                        case "SET_PIR" :
                            sbLbfh.setSetPir(z[1]);
                            break;
                        case "SET_DS" :
                            sbLbfh.setSetDs(z[1]);
                            break;
                    }
                }
                sbLbfhService.save(sbLbfh);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) ;
            serverSocket.close();
            System.out.println("客户端已断开连接。");
            lbsjk(port);
        }
    }
}
